package com.aidong.dao.wills;

import com.aidong.dao.base.OrderSet;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PayList;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by john on 16-11-3.
 */
@Repository
public class MemberEarnestDao extends BaseDao<MemberEarnest, Integer> {
    public MemberEarnestDao() {
    }

    public List<MemberEarnest> getListByMemberCode(String memberCode) {
        return this.queryList("memberCode=:p1", new Object[]{memberCode}, (new OrderSet()).desc("saleDate"), -1);
    }

    public List<MemberEarnest> getNotUseList(String shopCode, String earnestType, String memberCode) {
        return memberCode != null && memberCode.length() > 0 ? this.queryList("shopCode=:p1 and earnestType=:p2 and memberCode=:p3 and status=0 and signStatus<>2 and overDueDate>getdate()", new Object[]{shopCode, earnestType, memberCode}, (OrderSet) null, -1) : this.queryList("shopCode=:p1 and earnestType=:p2 and status=0 and signStatus<>2 and overDueDate>getdate()", new Object[]{shopCode, earnestType}, (OrderSet) null, -1);
    }

    public List<MemberEarnest> getNotUseListByName(String shopCode, String earnestType, String memberName) {
        return this.queryList("shopCode=:p1 and earnestType=:p2 and custName=:p3 and status=0 and signStatus<>2 and overDueDate>getdate()", new Object[]{shopCode, earnestType, memberName}, (OrderSet) null, -1);
    }

    public void addEarnest(MemberEarnest earnest, List<PayList> payList) {
        if ((earnest.getMemberCode() == null || earnest.getMemberCode().length() == 0) && earnest.getIdNumber() != null && earnest.getIdNumber().length() > 0) {
            MemberInfo sign = new MemberInfo();
            sign.setMemberSource("02");
            sign.setCardCode("");
            sign.setShopCode(earnest.getShopCode());
            sign.setChName(earnest.getCustName());
            sign.setGender(earnest.getGender());
            sign.setIdType(earnest.getIdType());
            sign.setIdNumber(earnest.getIdNumber());
            sign.setMobile(earnest.getMobile());
            sign.setMemberPhoto("");
            sign.setStatus(Integer.valueOf(1));
            sign.setCreateUser(earnest.getCreateUser());
            sign.setCreateTime(new Date());
            sign.setUpdateTime(new Date());
            String pay = sign.getShopCode();
            SimpleDateFormat sql = new SimpleDateFormat("yyMMdd");
            String memberPrefix = pay + sql.format(new Date());
            String sql1 = "select max(memberCode) from MemberInfo where shopCode=:p1 and memberCode like :p2";
            String maxCode = (String) this.getBySql(String.class, sql1, new Object[]{pay, memberPrefix + "%"});
            int maxId = 0;
            if (maxCode != null && maxCode.length() > pay.length()) {
                try {
                    maxId = Integer.parseInt(maxCode.substring(memberPrefix.length()));
                } catch (Exception var11) {
                    ;
                }
            }

            ++maxId;
            String memberCode = "0000" + maxId;
            memberCode = memberPrefix + memberCode.substring(memberCode.length() - 4);
            sign.setMemberCode(memberCode);
            this.getSession().save(sign);
            earnest.setMemberCode(memberCode);
            earnest.setCardCode("");
        }

        if (earnest.getMemberCode() == null) {
            earnest.setMemberCode("");
        }

        if (earnest.getCardCode() == null) {
            earnest.setCardCode("");
        }

        if (earnest.getIdNumber() == null) {
            earnest.setIdNumber("");
        }

        this.add(earnest);
        Iterator var12 = payList.iterator();

        while (var12.hasNext()) {
            PayList var14 = (PayList) var12.next();
            var14.setSaleId(earnest.getSaleId());
            var14.setContractCode(String.valueOf(earnest.getSaleId()));
            this.getSession().save(var14);
            if ("S011".equalsIgnoreCase(var14.getPayType())) {
                String var15 = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
                this.updateBySql(var15, new Object[]{Integer.valueOf(2), var14.getAutoId(), var14.getSalesCode(), var14.getSalesCode2(), new Date(), var14.getNote()});
            }
        }

        ContractSign var13 = new ContractSign();
        var13.setShopCode(earnest.getShopCode());
        var13.setContractCode(String.valueOf(earnest.getSaleId()));
        var13.setContractType("03");
        var13.setSaleId(earnest.getSaleId());
        var13.setMemberCode(earnest.getMemberCode());
        var13.setMemberName(earnest.getCustName());
        var13.setStatus(Integer.valueOf(1));
        var13.setSalesCode(earnest.getSalesCode());
        var13.setSalesCode2(earnest.getSalesCode2());
        var13.setCreateUser(earnest.getCreateUser());
        var13.setCreateTime(new Date());
        var13.setUpdateTime(new Date());
        this.getSession().save(var13);
    }

    public void addMergeEarnest(MemberEarnest earnest, List<MemberEarnest> oldList) {
        this.add(earnest);
        Iterator i$ = oldList.iterator();

        while (i$.hasNext()) {
            MemberEarnest oldEarnest = (MemberEarnest) i$.next();
            oldEarnest.setNewSaleId(earnest.getSaleId());
            this.update(oldEarnest);
        }

    }

    public void addBackEarnest(MemberEarnest earnest, MemberEarnest oldEarnest, List<PayList> payList) {
        this.add(earnest);
        Iterator i$ = payList.iterator();

        while (i$.hasNext()) {
            PayList pay = (PayList) i$.next();
            pay.setSaleId(earnest.getSaleId());
            this.getSession().save(pay);
            if ("S011".equalsIgnoreCase(pay.getPayType())) {
                String sql = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
                this.updateBySql(sql, new Object[]{Integer.valueOf(2), pay.getAutoId(), pay.getSalesCode(), pay.getSalesCode2(), new Date(), pay.getNote()});
            }
        }

        oldEarnest.setNewSaleId(earnest.getSaleId());
        this.update(oldEarnest);
    }
}

